package com.jprocms.module.cms.controller.directive;

import com.jprocms.module.cms.controller.admin.channel.vo.ChannelListReqVO;
import com.jprocms.module.cms.dal.dataobject.channel.ChannelDO;
import com.jprocms.module.cms.enums.EnumIntCommonStatus;
import com.jprocms.module.cms.service.channel.ChannelService;
import com.jprocms.module.cms.util.Freemarkers;
import com.jprocms.module.cms.util.FrontContextUtil;
import com.jprocms.module.cms.web.Directives;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 栏目列表 自定义标签
 *
 * @author jprocms
 */
public class ChannelListDirective implements TemplateDirectiveModel {
    /**
     * 站点ID。Long类型
     */
    private static final String SITE_ID = "siteId";
    /**
     * 上级栏目ID。Long类型
     */
    private static final String PARENT_ID = "parentId";
    /**
     * 上级栏目路径。字符串（String）
     */
    private static final String PARENT_PATH = "parentPath";
    /**
     * 是否过滤列表是否显示属性。布尔型（Boolean） 默认：true
     */
    private static final String FILTER_LIST = "filterList";
    /**
     * 是否包含子栏目。布尔型（Boolean）。默认：false
     */
    private static final String INCLUDE_CHILD = "includeChild";

    @SuppressWarnings("unchecked")
    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        Freemarkers.requireLoopVars(loopVars);
        Freemarkers.requireBody(body);
        Long defSiteId = FrontContextUtil.getSiteId(env);
        Long siteId = Directives.getLong(params, SITE_ID, defSiteId);
        Long parentId = Directives.getLong(params, PARENT_ID);
        String parentPath = Directives.getString(params, PARENT_PATH);
        //设置了parentPath 以 parentPath为准
        if (StringUtils.hasText(parentPath)) {
            ChannelDO channelDO = channelService.selectByChannelPath(parentPath, siteId);
            if (channelDO != null) {
                parentId = channelDO.getId();
            }
        }
        Boolean filterList = Freemarkers.getBoolean((TemplateModel) params.get(FILTER_LIST), true);
        Boolean includeChild = Freemarkers.getBoolean((TemplateModel) params.get(INCLUDE_CHILD), false);
        ChannelListReqVO channelListReqVO = ChannelListReqVO.of(siteId.longValue(), parentId, includeChild, filterList ? EnumIntCommonStatus.OPEN.getStatus() : null);
        List<ChannelDO> list = channelService.getChannelListForFront(channelListReqVO);
        loopVars[0] = env.getObjectWrapper().wrap(list);
        body.render(env.getOut());
    }


    private final ChannelService channelService;

    public ChannelListDirective(ChannelService channelService) {
        this.channelService = channelService;
    }
}
